package com.lianlian.controller;

import com.lianlian.file.FastDFSFile;
import com.lianlian.util.FastDFSUtil;
import entity.Result;
import entity.StatusCode;
import io.netty.util.internal.StringUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping(value = "/upload")
@CrossOrigin
public class FileUploadController {
    /**
     * 文件上传
     */
    @PostMapping
    public Result upload(@RequestParam(value = "file")MultipartFile file) throws Exception {
        //封装文件信息
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),//文件名字 1.jpg
                file.getBytes(),          //文件字节数组
                StringUtils.getFilenameExtension(file.getOriginalFilename()) //获取文件后缀名
        );
        //调用工具FastDFSUtil工具类将文件传入到FestDFS中
        String[] uploads=FastDFSUtil.upload(fastDFSFile);
        //拼接一个访问地址
        String url = FastDFSUtil.getTrackerInfo()+uploads[0]+"/"+uploads[1];
        return new Result(true, StatusCode.OK,"文件上传成功!",url);
    }
}
